﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = UserName.Text;
            string pwd = Password.Text;
            string constr = "server=.;uid=sa;pwd=123456;database=ASB";
            SqlConnection con = new SqlConnection(constr);
            string sql = "select * from Student where Name=@name and pwd=@pwd";
            SqlParameter[] para = { new SqlParameter("@name", name), new SqlParameter("@pwd",pwd)};
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddRange(para);
                SqlDataReader sdr = cmd.ExecuteReader();
                if (sdr.Read())
                {
                    Session["CurrentUserName"] = name;
                    Literal1.Text = "登录成功！";
                    Response.Redirect("WebForm2.aspx");
                }
                else
                {
                    Literal1.Text = "账号或密码错误！";
                }
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message.ToString());
            }
            finally 
            {
                if (con!=null)
                {
                    con.Close();
                }
            }
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebForm3.aspx");
        }
    }
}